/*
 * @Descripttion: 
 * @version: 
 * @Author: TT
 * @Date: 2023-09-06 16:35:05
 * @LastEditors: TT-hzy
 * @LastEditTime: 2024-08-11 13:33:04
 */

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import 'package:pretty_dio_logger/pretty_dio_logger.dart';

import '../../commpontents/common_widgets.dart';
import '../../pages/my/config/config_index.dart';
import '../../utils/dio_cache_tool.dart';
import '../../utils/package_info_tool.dart';
import '../../utils/shared_preferences_tool.dart';
import '../config_index.dart';

import '../transformers/default_interceptor.dart';

class GlobalConfig {
  static init() async {
    WidgetsFlutterBinding.ensureInitialized();
    initLaunch();
    await initThirdParty();
    await initNetWork();
    await initNormalDefault();
  }

  /// 初始化第三发
  static initThirdParty() async {
    await PackageInfoTool.fromPlateform();
    await SharedPreferencesTool.getInstance();
    if (!kIsWeb) {
      await DioCacheTool.initCacheStore();
    }
  }

  /// 配置默认数据
  static initNormalDefault() async {
    HzyPlaceHolderConfig.loadWidget = configLoadWidget();
  }

  /// 配置国际化
  static initLaunch() {
    LaunchzhCN.getInstance().configAllLaunch(
      list: [
        zhCN,
        myzhCN,
        noteZhCN,
      ],
    );

    LaunchenUS.getInstance().configAllLaunch(
      list: [
        enUS,
        myenUS,
        noteEnUS,
      ],
    );
  }

  /// 配置网络请求
  static initNetWork() async {
    /**
     * 后台返回数据格式
     * {
     *  code:200,
     *  message:"成功",
     *  result:{}
     * }
     * 
    */
    HzyNormalDefaultTransFormer.getInstance().setHttpConfig(
      config: HzyNormalHttpResponeConfig(
        status: "code",
        code: "code",
        msg: "msg",
        data: "data",
        successCode: 200,
      ),
    );
    List<Interceptor>? interceptors = [
      /// 默认拦截器.用于你项目中通用网络事件处理
      DefaultInterceptor(),

      /// 请求内容格式化 拦截器
      PrettyDioLogger(
        requestBody: true,
        requestHeader: true,
      ),
    ];

    if (!kIsWeb) {
      interceptors.add(DioCacheTool.init());
    }
    // 注入网络请求组件
    Get.put(
      HzyNormalClient(
        normalHttpConfig: HzyNormalHttpConfig(
          baseUrl: HttpNormalConfig.configbaseUrl(),
          isNeedLog: false,
          interceptors: interceptors,
        ),
      ),
      permanent: true,
      tag: HzyNormalClient.defaultClientTag,
    );
  }
}
